﻿package com.shuqi.common.utils;

import java.io.File;
import java.security.MessageDigest;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

/**
 * Log工具主类
 * 
 * @author yhw
 * @version 1.0
 */
public class Log4an {
    private final static String NULL = "null";
    private final static String ZERO = "0";
    private final static String ONE = "1";

    /**
     * 是否显示log:1,显示
     */
    private static String IS_SHOW_LOG;

    /**
     * 使用前必须初始化的方法
     * 
     * @param context Context
     */
    public static void init(Context context, String path) {
        Log.v("Log4an", "init() isShow = " + IS_SHOW_LOG);
        if (IS_SHOW_LOG != null) {
            return;
        }
        Log.v("Log4an", "init() start");
        ApplicationInfo info = null;
        File file = null;
        try {
            if (TextUtils.isEmpty(path)) {
                file = Environment.getExternalStorageDirectory();
            } else {
                file = new File(path);
            }
            if (file != null && !file.exists()) {
                file.mkdirs();
            }
            String filename;

            TelephonyManager tm =
                    (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            filename = MD5(tm.getDeviceId());

            file = new File(file.getAbsolutePath(), filename);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            info =
                    context.getPackageManager().getApplicationInfo(context.getPackageName(),
                            PackageManager.GET_META_DATA);
            // 是debug模式
            if ((info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
                IS_SHOW_LOG = ONE;
                if (!file.exists()) {
                    file.createNewFile();
                }
                // 不是
            } else {
                if (file.exists()) {
                    IS_SHOW_LOG = ONE;
                } else {
                    IS_SHOW_LOG = ZERO;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        Log.v("Log4an", "init() end : " + IS_SHOW_LOG);
    }

    /**
     * 显示tag为"Log4an"的verbose级别Log
     * 
     * @param content 显示内容
     */
    public static void v(String content) {
        v("Log4an", content);
    }

    /**
     * 显示tag为"Log4an"的debug级别Log
     * 
     * @param content 显示内容
     */
    public static void d(String content) {
        d("Log4an", content);
    }

    /**
     * 显示tag为"Log4an"的info级别Log
     * 
     * @param content 显示内容
     */
    public static void i(String content) {
        i("Log4an", content);
    }

    /**
     * 显示tag为"Log4an"的warn级别Log
     * 
     * @param content 显示内容
     */
    public static void w(String content) {
        w("Log4an", content);
    }

    /**
     * 显示tag为"Log4an"的error级别Log
     * 
     * @param content 显示内容
     */
    public static void e(String content) {
        e("Log4an", content);
    }

    /**
     * 显示verbose级别的Log
     * 
     * @param tag Log的标签
     * @param content 显示内容
     */
    public static void v(String tag, String content) {
        if (IS_SHOW_LOG != null && ONE.equals(IS_SHOW_LOG)) {
            Log.v(tag == null ? NULL : tag, content == null ? NULL : content);
        }
    }

    /**
     * 显示debug级别的Log
     * 
     * @param tag Log的标签
     * @param content 显示内容
     */
    public static void d(String tag, String content) {
        if (IS_SHOW_LOG != null && ONE.equals(IS_SHOW_LOG)) {
            Log.d(tag == null ? NULL : tag, content == null ? NULL : content);
        }
    }

    /**
     * 显示info级别的Log
     * 
     * @param tag Log的标签
     * @param content 显示内容
     */
    public static void i(String tag, String content) {
        if (IS_SHOW_LOG != null && ONE.equals(IS_SHOW_LOG)) {
            Log.i(tag == null ? NULL : tag, content == null ? NULL : content);
        }
    }

    /**
     * 显示warn级别的Log
     * 
     * @param tag Log的标签
     * @param content 显示内容
     */
    public static void w(String tag, String content) {
        if (IS_SHOW_LOG != null && ONE.equals(IS_SHOW_LOG)) {
            Log.w(tag == null ? NULL : tag, content == null ? NULL : content);
        }
    }

    /**
     * 显示error级别的Log
     * 
     * @param tag Log的标签
     * @param content 显示内容
     */
    public static void e(String tag, String content) {
        if (IS_SHOW_LOG != null && ONE.equals(IS_SHOW_LOG)) {
            Log.e(tag == null ? NULL : tag, content == null ? NULL : content);
        }
    }

    /** 获取MD5编码 */
    public static final String MD5(String s) {
        try {
            byte[] btInput = s.getBytes();
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            mdInst.update(btInput);
            byte[] md = mdInst.digest();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < md.length; i++) {
                int val = ((int) md[i]) & 0xff;
                if (val < 16)
                    sb.append(ZERO);
                sb.append(Integer.toHexString(val));
            }
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
